package com.jwaoo.chat.session.recent.adpter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;

import com.jwaoo.chat.R;
import com.jwaoo.chat.fragment.session.bean.SessionBean;
import com.jwaoo.chat.session.recent.SessionCallback;
import com.jwaoo.chat.session.recent.holder.SessionViewHolder;
import com.jwaoo.chat.ui.recyclerview.adpter.BaseMultiItemQuickAdapter;
import com.jwaoo.chat.ui.recyclerview.holder.BaseViewHolder;

import java.util.List;

/**
 * Created by huangjun on 2016/12/11.
 */

public class SessionAdapter extends BaseMultiItemQuickAdapter<SessionBean, BaseViewHolder> {

    private Activity activity;
    interface ViewType {
        int VIEW_TYPE_COMMON = 1;
    }

    private SessionCallback callback;

    public SessionAdapter(RecyclerView recyclerView, List<SessionBean> data) {
        super(recyclerView, data);
        addItemType(ViewType.VIEW_TYPE_COMMON, R.layout.fragment_contacts, SessionViewHolder.class);
    }

    public void setActivity(Activity activity){
        this.activity = activity;
    }

    public Activity getActivity(){
        return this.activity;
    }

    @Override
    protected int getViewType(SessionBean item) {
        return ViewType.VIEW_TYPE_COMMON;
    }

    @Override
    protected String getItemKey(SessionBean item) {
        StringBuilder sb = new StringBuilder();
        sb.append(item.getSessiontype()).append("_").append(item.getFriend());
        return sb.toString();
    }

    public SessionCallback getCallback() {
        return callback;
    }

    public void setCallback(SessionCallback callback) {
        this.callback = callback;
    }

    public void deleteItem(final SessionBean sessionBean) {
        if (sessionBean == null) {
            return;
        }
        this.activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                getData().remove(sessionBean);
                notifyDataSetChanged();
            }
        });
    }
}
